﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using DevExpress.XtraEditors;


namespace pTools.cMethods
{
    public class softUpdate
    {
        DBHelper helper = new DBHelper();
        StringBuilder sb;
        public bool cancelUpdate;


        public softUpdate() { }


        //检查版本更新
        public bool checkUpdate(bool check)
        {
            bool b = false;
            cancelUpdate = false;
            sb = new StringBuilder();
            string targetVersion;

            //获取最新版本号
            sb.Append("select Version from sys_version order by id desc");
            var table = helper.getData(sb.ToString());
            if (table.Rows.Count > 0)
            {
                targetVersion = table.Rows[0][0].ToString();
            }
            else
            {
                return b;
            }

            //获得主程序的版本号
            string version = Application.ProductVersion.ToString();

            //var a = targetVersion.Equals(version);

            //MessageBox.Show(targetVersion + "        " + version + "            " + (targetVersion.Equals(version)).ToString());

            if (!targetVersion.Equals(version))
            {
                b = true;
                if (MessageBox.Show("检测到新版本，是否立即更新？", "版本检查", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Process process = new Process();
                    process.StartInfo.FileName = "DownProject.exe";
                    process.StartInfo.CreateNoWindow = true;
                    //ProcessStartInfo startInfo = new ProcessStartInfo("DownProject.exe");
                    //process.StartInfo = startInfo;
                    //开始运行
                    process.Start();
                }
                else
                {
                    cancelUpdate = true;
                }
            }
            else
            {
                if (check)
                    XtraMessageBox.Show("当前已为最新版本！");
            }

            return b;
        }


    }
}
